﻿

//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "CmdRemoveFamily.h"
#include "IUiDocument.h"
#include "FamilyUtils.h"
#include "UiCommonDialog.h"
#include "IUserTransaction.h"
#include "CommandParameters.h"
#include "IUiDocumentViewManager.h"

#include "CommandIds.h"
#include "DocumentUtils.h"
#include "CommandRegister.h"
#include "UiPlatformUtils.h"
#include "IRibbonContextualPage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_COMMAND(CmdRemoveFamily);

Sample::CmdRemoveFamily::CmdRemoveFamily(void)
    : CommandBase(ID_CMD_FAMILY_REMOVE, false)
{
}

Sample::CmdRemoveFamily::~CmdRemoveFamily(void)
{
}

bool Sample::CmdRemoveFamily::IsEnabled() const
{
    return true;
}
bool Sample::CmdRemoveFamily::IsVisible() const
{
    return true;
}

gcmp::OwnerPtr<IAction> Sample::CmdRemoveFamily::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS((int)cmdParams.size() == 1, L"CmdLoadFamilyToProject没有参数",L"GDMP",L"2023-11-20");

    //获得当前的族文档
    IUiDocument* pUIDoc = IUiDocumentViewManager::Get()->GetCurrentUiDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pUIDoc, L"当前UI文档为空",L"GDMP",L"2023-11-20");
    gcmp::IDocument* pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"Document为空",L"GDMP",L"2023-11-20");

    auto itor = cmdParams.find(L"FamilyName");
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(itor != cmdParams.end(), L"所需参数不存在",L"GDMP",L"2023-11-20");
    std::wstring familyName = itor->second.AsWString();
    auto& instanceIds = DocumentUtils::GetFamilyInstances(pDoc, familyName);
    if (instanceIds.size() > 0)
    {
        UiCommonDialog::ButtonType buttonType = UiCommonDialog::ShowMessageBox(GBMP_TR(L"删除构件"),
            GBMP_TR(L"删除构件的同时， 构件实例也将被删除"), (int)UiCommonDialog::ButtonType::OK | (int)UiCommonDialog::ButtonType::Cancel);
        if (buttonType != UiCommonDialog::ButtonType::OK)
        {
            return nullptr;
        }
    }

    gcmp::OwnerPtr<IUserTransaction> opUT = IUserTransaction::Create(pDoc, (GBMP_TR(L"删除Family")));
    FamilyUtils::DeleteFamily(pDoc, familyName);
    opUT->Commit();

    return nullptr;
}
